//
//  WPFindPayPwdViewController.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/1.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPFindPayPwdViewController.h"
#import "WPGetTestCode.h"
#import "WPFindPayPwdViewModel.h"

@interface WPFindPayPwdViewController ()
@property (nonatomic, strong) UILabel * introLabel;/**< 说明文本 */
@property (nonatomic, strong) UIView * accountBackView;/**< 账号输入框背景图 */
@property (nonatomic, strong) UITextField * accountTextField;/**< 账号输入框 */
@property (nonatomic, strong) UIView * codeBackView;/**< 验证码背景图 */
@property (nonatomic, strong) UITextField * codeTextField;/**< 验证码输入框 */
@property (nonatomic, strong) UIButton * getCodeBtn;/**< 获取验证码按钮 */
@property (nonatomic, strong) UIView * pwdBackView;/**< 密码输入框背景 */
@property (nonatomic, strong) UITextField * pwdTextField;/**< 新密码输入框 */
@property (nonatomic, strong) UILabel * pwdIntroLabel;/**< 密码设置说明 */
@property (nonatomic, strong) UIButton * submitStepBtn;/**< 立即登录按钮 */
@property (nonatomic, strong) NSTimer * timer;/**< 计时器 */
@property (nonatomic, assign) NSInteger timeCount;/**< 时间记录 */
- (void)initializeUserInterface; /**< 初始化用户界面 */
@end

@implementation WPFindPayPwdViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeUserInterface];
}
#pragma mark - init
- (void)initializeUserInterface {
    self.title = @"找回支付密码";
    [self.view addSubview:self.introLabel];
    [self.view addSubview:self.accountBackView];
    [self.view addSubview:self.accountTextField];
    [self.view addSubview:self.codeBackView];
    [self.view addSubview:self.codeTextField];
    [self.view addSubview:self.getCodeBtn];
    [self.view addSubview:self.pwdBackView];
    [self.view addSubview:self.pwdTextField];
    [self.view addSubview:self.pwdIntroLabel];
    [self.view addSubview:self.submitStepBtn];
}
#pragma mark - responds events
- (void)textFieldDidChange:(UITextField *)textField {
    if (textField == _codeTextField && textField.text.length > 6) {
        textField.text = [textField.text substringFromIndex:6];
    }
    if (textField == _pwdTextField && textField.text.length > 14) {
        textField.text = [textField.text substringToIndex:14];
    }
}
- (void)respondToGetCodeBtn:(UIButton *)sender {
    [self.view endEditing:YES];
    [sender setTitle:@"60s再次获取" forState:UIControlStateNormal];
    sender.userInteractionEnabled = NO;
    [self.timer setFireDate:[NSDate distantPast]];
    _timeCount = 60;
    [WPGetTestCode getTestCodeWithPhone:_accountTextField.text andSuccessBlock:^(NSString *result) {
    } andFailureBlock:^(NSString *fail) {
    }];
}
- (void)respondsToTimer {
    self.timeCount--;
    NSString * timeStr = [NSString stringWithFormat:@"%lds再次获取", (long)_timeCount];
    [_getCodeBtn setTitle:timeStr forState:UIControlStateNormal];
    if (_timeCount == 0) {
        [self.timer setFireDate:[NSDate distantFuture]];
        [_getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        _getCodeBtn.userInteractionEnabled = YES;
    }
}
- (void)respondToSubmitBtn {
    [self.view endEditing:YES];
    if (_codeTextField.text.length == 0) {
        [self initializeAlertControllerWithMessageAndDismiss:@"请输入验证码"];
        return;
    }
    if (_pwdTextField.text.length == 0) {
        [self initializeAlertControllerWithMessageAndDismiss:@"请输入新密码"];
        return;
    }
    __weak typeof(self)weakself = self;
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.removeFromSuperViewOnHide = YES;
    hud.color = [UIColor lightGrayColor];
    hud.activityIndicatorColor = [UIColor blackColor];
    hud.labelText = @"请稍候";
    hud.detailsLabelText = @"正在重置密码...";
    [WPFindPayPwdViewModel findPwdWithPhone:_accountTextField.text andCode:_codeTextField.text andPwd:_pwdTextField.text andSuccessBlock:^(NSString *result) {
        hud.labelText = result;
        hud.detailsLabelText = nil;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [hud hide:YES];
            [weakself.navigationController popViewControllerAnimated:YES];
        });
    } andFailureBlock:^(NSString *fail) {
        hud.labelText = fail;
        hud.detailsLabelText = @"请稍候再试";
        [hud hide:YES afterDelay:2.0];
    }];
}
#pragma mark - getter
- (UILabel *)introLabel {
    if (!_introLabel) {
        _introLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), navBar_Fheight, 300*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            lab.text = @"请在下面填写相关信息:";
            lab;
        });
    }
    return _introLabel;
}
- (UIView *)accountBackView {
    if (!_accountBackView) {
        _accountBackView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.introLabel.frame), SCREEN_SIZE.width, 35*DHFlexibleHorizontalMutiplier())];
            view.backgroundColor = [UIColor whiteColor];
            view;
        });
    }
    return _accountBackView;
}
- (UITextField *)accountTextField {
    if (!_accountTextField) {
        _accountTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), CGRectGetMinY(self.accountBackView.frame), 300*DHFlexibleHorizontalMutiplier(), CGRectGetHeight(self.accountBackView.bounds))];
            text.leftViewMode = UITextFieldViewModeAlways;
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            label.text = @"账号";
            label.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            text.leftView = label;
            text.clearButtonMode = UITextFieldViewModeWhileEditing;
            text.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            text.userInteractionEnabled = NO;
            text.text = [UserModel defaultUser].phoneNumber;
            text;
        });
    }
    return _accountTextField;
}
- (UIView *)codeBackView {
    if (!_codeBackView) {
        _codeBackView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.accountBackView.frame)+10*DHFlexibleHorizontalMutiplier(), SCREEN_SIZE.width, 35*DHFlexibleHorizontalMutiplier())];
            view.backgroundColor = [UIColor whiteColor];
            view;
        });
    }
    return _codeBackView;
}
- (UITextField *)codeTextField {
    if (!_codeTextField) {
        _codeTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), CGRectGetMinY(self.codeBackView.frame), 220*DHFlexibleHorizontalMutiplier(), CGRectGetHeight(self.accountBackView.bounds))];
            text.leftViewMode = UITextFieldViewModeAlways;
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            label.text = @"验证码";
            label.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            text.leftView = label;
            text.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            text.clearButtonMode = UITextFieldViewModeWhileEditing;
            [text addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
            text.keyboardType = UIKeyboardTypeNumberPad;
            text.placeholder = @"请输入短信验证码";
            text;
        });
    }
    return _codeTextField;
}
- (UIButton *)getCodeBtn {
    if (!_getCodeBtn) {
        _getCodeBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(240*DHFlexibleHorizontalMutiplier(), CGRectGetMinY(self.codeTextField.frame)+5*DHFlexibleHorizontalMutiplier(), 70*DHFlexibleHorizontalMutiplier(), 25*DHFlexibleHorizontalMutiplier());
            btn.backgroundColor = COLOR_RGB(225, 225, 225, 1);
            btn.titleLabel.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            [btn setTitle:@"获取验证码" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(respondToGetCodeBtn:) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _getCodeBtn;
}
- (UIView *)pwdBackView {
    if (!_pwdBackView) {
        _pwdBackView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.codeBackView.frame)+10*DHFlexibleHorizontalMutiplier(), SCREEN_SIZE.width, 35*DHFlexibleHorizontalMutiplier())];
            view.backgroundColor = [UIColor whiteColor];
            view;
        });
    }
    return _pwdBackView;
}
- (UITextField *)pwdTextField {
    if (!_pwdTextField) {
        _pwdTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.codeBackView.frame)+ 10*DHFlexibleHorizontalMutiplier(), 220*DHFlexibleHorizontalMutiplier(), CGRectGetHeight(self.accountBackView.bounds))];
            text.leftViewMode = UITextFieldViewModeAlways;
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            label.text = @"密码";
            label.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            text.leftView = label;
            text.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            text.clearButtonMode = UITextFieldViewModeWhileEditing;
            [text addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
            text.secureTextEntry = YES;
            text.placeholder = @"请输入您的新密码";
            text;
        });
    }
    return _pwdTextField;
}
- (UILabel *)pwdIntroLabel {
    if (!_pwdIntroLabel) {
        _pwdIntroLabel = ({
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.pwdTextField.frame), 310*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier())];
            label.text = @"6-14位密码，建议数字，字母，符号组合";
            label.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            label;
        });
    }
    return _pwdIntroLabel;
}
- (UIButton *)submitStepBtn {
    if (!_submitStepBtn) {
        _submitStepBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(10*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.pwdIntroLabel.frame)+35*DHFlexibleHorizontalMutiplier(), 300*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier());
            btn.backgroundColor = COLOR_RGB(234, 27, 47, 1);
            btn.layer.cornerRadius = 15*DHFlexibleHorizontalMutiplier();
            [btn setTitle:@"提交" forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(respondToSubmitBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _submitStepBtn;
}

@end
